package com.code.handler;

import cn.dev33.satoken.exception.NotLoginException;
import com.code.exception.AppException;
import com.code.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IllegalArgumentException.class)
    public Result<String> IAExceptionHandler(Exception e){
        log.error("IllegalArgumentExceptionHandler:{}",e.getMessage(),e);
        return Result.error("服务器异常，参数错误");
    }

    @ExceptionHandler(Exception.class)
    public Result<String> exceptionHandler(Exception e){
        if(e instanceof AppException){
            log.error("AppException:{}",e.getMessage());
            AppException appException = (AppException) e;
            return Result.other(appException.getCode(), appException.getMsg(), null);
        }
        if(e instanceof NotLoginException){
            log.error("NotLoginException:{}",e.getMessage());
            return Result.other(50000,"token过期",null);
        }
        log.error("ExceptionHandler:{}",e.getMessage(),e);
        return Result.error("服务器异常");
    }

    @ExceptionHandler(SQLException.class)
    public Result<String> sqlExceptionHandler(SQLException e){
        log.error("SQLExceptionHandler:{}",e.getMessage(),e);
        return Result.error("服务器数据库异常");
    }
    
}